package carmesi.wizards;

import java.awt.Component;
import java.util.Map;
import org.netbeans.api.java.project.JavaProjectConstants;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.SourceGroup;
import org.netbeans.api.project.Sources;
import org.netbeans.spi.java.project.support.ui.templates.JavaTemplates;
import org.netbeans.spi.project.ui.templates.support.Templates;
import org.openide.WizardDescriptor.Panel;

/**
 *
 * @author Victor
 */
public class URLControllerWizardIterator extends CarmesiWizardIterator implements ComponentValidator<Component> {
    private URLControllerComponent component;

    @Override
    protected Panel[] createPanels() {
        Project project = Templates.getProject(getWizardDescriptor());
        Sources sources = project.getLookup().lookup(Sources.class);
        SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
        component=new URLControllerComponent(getWizardDescriptor());
        component.setComponentValidator(this);
        return new Panel[]{JavaTemplates.createPackageChooser(project, groups, component)};
    }

    @Override
    protected void setTemplateArguments(Map<String, String> args) {
        args.put("url", component.getURLValue());
        String view=component.getURLView();
        args.put("viewURL", view);
        args.put("annotationType", component.isForwardToView() ? "ForwardTo" : "RedirectTo");
        args.put("annotationClass", component.isForwardToView() ? "carmesi.ForwardTo" : "carmesi.RedirectTo");
    }

    @Override
    public void validate(Component panel) throws IllegalArgumentException {
        if(component.getURLValue().trim().equals("")){
            throw new IllegalArgumentException("URL must not be empty");
        }
    }

}
